package com.zjb.home.boxingtu.view.other;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zjb.home.boxingtu.R;
import com.zjb.home.boxingtu.bean.LableImpl;
import com.zjb.home.boxingtu.view.BaseFramelayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by zhangjiebo on 2020/4/3.
 *
 * @author ZhangJieBo
 */
public class LableView extends BaseFramelayout {
    @BindView(R.id.viewAll)
    LinearLayout viewAll;
    private int width;
    private float textSize;
    private int textColor;
    private Drawable lableBackground;
    /**
     * Don't show any dividers.
     */
    public static final int SHOW_DIVIDER_NONE = 0;
    /**
     * Show a divider at the beginning of the group.
     */
    public static final int SHOW_DIVIDER_BEGINNING = 1;
    /**
     * Show dividers between each item in the group.
     */
    public static final int SHOW_DIVIDER_MIDDLE = 2;
    /**
     * Show a divider at the end of the group.
     */
    public static final int SHOW_DIVIDER_END = 4;
    private int showDividersH;
    private float lablePadding;
    private float lablePaddingStart;
    private float lablePaddingEnd;
    private float lablePaddingTop;
    private float lablePaddingBottom;
    private Drawable dividerW;
    private Drawable dividerH;
    private int showDividersW;
    public interface OnItemClickListener{
        void itemClick(int position);
    }

    OnItemClickListener onItemClickListener;

    public LableView(@NonNull Context context) {
        super(context);
    }

    public LableView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LableView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.LableView);
        textSize = typedArray.getDimension(R.styleable.LableView_textSize, getResources().getDimension(R.dimen.m12));
        textColor = typedArray.getColor(R.styleable.LableView_textColor, Color.BLACK);
        lableBackground = typedArray.getDrawable(R.styleable.LableView_lableBackground);
        lablePadding = typedArray.getDimension(R.styleable.LableView_lablePadding, 0);
        lablePaddingStart = typedArray.getDimension(R.styleable.LableView_lablePaddingStart, 0);
        lablePaddingEnd = typedArray.getDimension(R.styleable.LableView_lablePaddingEnd, 0);
        lablePaddingTop = typedArray.getDimension(R.styleable.LableView_lablePaddingTop, 0);
        lablePaddingBottom = typedArray.getDimension(R.styleable.LableView_lablePaddingBottom, 0);
        showDividersH = typedArray.getInt(R.styleable.LableView_showDividersH, SHOW_DIVIDER_MIDDLE);
        showDividersW = typedArray.getInt(R.styleable.LableView_showDividersW, SHOW_DIVIDER_MIDDLE);
        dividerW = typedArray.getDrawable(R.styleable.LableView_dividerW);
        dividerH = typedArray.getDrawable(R.styleable.LableView_dividerH);
        typedArray.recycle();
    }

    @Override
    public void init() {
        inflate(getContext(), R.layout.view_lable, this);
        ButterKnife.bind(this);
        viewAll.setShowDividers(showDividersH);
        viewAll.setDividerDrawable(dividerH);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void addLable(LableImpl lable,int index) {
        if (viewAll.getChildCount() == 0) {
            LinearLayout linearLayout = getLinearLayout();
            TextView textView = getTextView(lable,index);
            linearLayout.addView(textView);
            viewAll.addView(linearLayout);
        } else {
            LinearLayout childLinear = (LinearLayout) viewAll.getChildAt(viewAll.getChildCount() - 1);
            TextView textView = getTextView(lable,index);
            textView.measure(0, 0);
            int leftWidth = getMeasuredWidth();
            for (int i = 0; i < childLinear.getChildCount(); i++) {
                View childAt = childLinear.getChildAt(i);
                childAt.measure(0, 0);
                leftWidth = leftWidth - childAt.getMeasuredWidth();
            }
            if (leftWidth >= textView.getMeasuredWidth()) {
                childLinear.addView(textView);
            } else {
                LinearLayout linearLayout = getLinearLayout();
                linearLayout.addView(textView);
                viewAll.addView(linearLayout);
            }
        }
    }

    private LinearLayout getLinearLayout() {
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setDividerDrawable(dividerW);
        linearLayout.setShowDividers(showDividersW);
        return linearLayout;
    }

    private TextView getTextView(LableImpl lable,int index) {
        TextView textView = new TextView(getContext());
        textView.setTag(index);
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setText(lable.getName());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        textView.setPadding((int) (lablePaddingStart > 0 ? lablePaddingStart : lablePadding), (int) (lablePaddingTop > 0 ?
                        lablePaddingTop : lablePadding), (int) (lablePaddingEnd > 0 ? lablePaddingEnd : lablePadding),
                (int) (lablePaddingBottom > 0 ? lablePaddingBottom : lablePadding));
        textView.setBackground(lableBackground);
        textView.setTextColor(textColor);
        textView.setOnClickListener(v -> {
            if (onItemClickListener!=null){
                onItemClickListener.itemClick((Integer) v.getTag());
            }
        });
        return textView;
    }

    public void addLables(List<LableImpl> lables) {
        post(() -> {
            for (int i = 0; i < lables.size(); i++) {
                addLable(lables.get(i),i);
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
